package quq.missq.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import quq.missq.BaseActivity;
import quq.missq.R;
/**
 * Created by wcx on 2015/9/21.
 */
public class MessageSettingActivity extends BaseActivity{

    private TextView tv_save;
    private SharedPreferences miss;
    private int imagesId[]={R.id.iv_1,R.id.iv_2,R.id.iv_3};
    private int rlLayoutId[]={R.id.rl_msg1,R.id.rl_msg2,R.id.rl_msg3};
    private int temp;
    private boolean isClick=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messagesetting);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        for (int i = 0; i <rlLayoutId.length;i++) {
            findViewById(rlLayoutId[i]).setOnClickListener(new MessageClickListener(i));
        }
        //0 开启 1 只在夜间开启  2 关闭
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                miss.edit().putInt("position",temp).commit();
                finish();
            }
        });

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    class MessageClickListener implements View.OnClickListener{
        private int position;
        public MessageClickListener(int position){
            this.position=position;
        }

        @Override
        public void onClick(View v) {
             isClick=true;
             show(position);
             temp=position;
             Log.i("wcx",temp+"temp");
            if(position==0&&isClick){
                openDialog();
            }
        }
    }

    private void initData() {
        int position = miss.getInt("position", 0);
        show(position);
    }


    private void initView() {
        miss = getSharedPreferences("Miss", MODE_PRIVATE);
        tv_save = (TextView) findViewById(R.id.tv_save);
    }

    public void show(int position){
        for (int i = 0; i <imagesId.length; i++) {
            if(position==i){
                findViewById(imagesId[i]).setVisibility(View.VISIBLE);
            } else {
                findViewById(imagesId[i]).setVisibility(View.GONE);
            }
        }
    }

    String desc="此设置会导致全天都处于免打扰模式,不会再收到推送消息,是否继续?";
    private void openDialog() {
        new AlertDialog.Builder(this).setTitle("提示").
                setMessage(desc).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }
}
